package site.jlopen.service.imp;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import site.jlopen.domain.job.KusciaJobResult;
import site.jlopen.domain.job.event.KusciaJobResultEvent;
import site.jlopen.domain.project.event.KusciaProjectEvent;
import site.jlopen.domain.project.event.KusciaProjectFlowEvent;
import site.jlopen.entity.job.TaskResultDataVo;
import site.jlopen.mapper.job.KusciaJobResultMapper;
import site.jlopen.mapper.project.KusciaProjectFlowMapper;
import site.jlopen.mapper.project.KusciaProjectMapper;
import site.jlopen.service.intf.project.JobResultService;
import site.jlopen.utils.StringUtil;
import site.jlopen.utils.id.IdUtils;

@Service
public class JobResultServiceImpl implements JobResultService {

	@Autowired private KusciaJobResultMapper resultMapper;
	@Autowired private KusciaProjectMapper projectMapper;
	@Autowired private KusciaProjectFlowMapper flowMapper;
	
	@Override
	public List<KusciaJobResultEvent> getResultList(Long userId, Long projectId, Long flowId, String type) {
		Map<String, Object> records = new HashMap<String, Object>();
		records.put("resultProjectId", projectId);
		records.put("projectUserId", userId);
		if(null != flowId) {
			records.put("resultFlowId", flowId);
		}
		if(StringUtil.isNotBlank(type)) {
			records.put("resultType", type);
		}
		List<KusciaJobResultEvent> list = resultMapper.selectAllNotPage(records);
		if(null != list) {
			for (KusciaJobResultEvent event : list) {
				KusciaProjectEvent project = projectMapper.selectByKey(event.getResultProjectId());
				if(null != project) {
					event.setProjectName(project.getProjectName());
				}
				KusciaProjectFlowEvent flow = flowMapper.selectByKey(event.getResultFlowId());
				if(null != flow) {
					event.setFlowName(flow.getFlowTitle());
				}
			}
		}
		return list;
	}

	@Override
	public List<TaskResultDataVo> getResultVos(Long userId, Long projectId, Long flowId, String type) {
		List<TaskResultDataVo> res = new ArrayList<TaskResultDataVo>();
		Map<String, Object> records = new HashMap<String, Object>();
		records.put("resultProjectId", projectId);
		records.put("projectUserId", userId);
		if(null != flowId) {
			records.put("resultFlowId", flowId);
		}
		if(StringUtil.isNotBlank(type)) {
			records.put("resultType", type);
		}
		List<KusciaJobResultEvent> list = resultMapper.selectAllNotPage(records);
		if(null != list) {
			for (KusciaJobResultEvent event : list) {
				TaskResultDataVo vo = new TaskResultDataVo();
				vo.setGraphCode(event.getResultGraphCode());
				vo.setDomainDataId(event.getResultDomaindataId());
				vo.setName(event.getResultName());
				res.add(vo);
			}
		}
		return res;
	}

	@Override
	public KusciaJobResultEvent getResultById(Long resultId) {
		KusciaJobResultEvent info = resultMapper.selectByKey(resultId);
		return info;
	}

	@Override
	public KusciaJobResultEvent getResultByGraph(String graphId) {
		KusciaJobResultEvent info = resultMapper.selectByInfo(new KusciaJobResult().setResultGraphId(graphId));
		return info;
	}

	@Override
	public boolean saveResult(KusciaJobResult result) {
		result.setResultId(IdUtils.SnowFlakeId());
		result.setResultCreate(new Date());
		int row = resultMapper.insertSelective(result);
		return row > 0? true: false;
	}

}
